//
//  LuBinCollectionViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/24.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "LuBinCollectionViewController.h"
#import "LuBDefine.h"
#import "CoreDataManager.h"
#import "LuBinCollection.h"
#import "AlbumViewController.h"

@interface LuBinCollectionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)CoreDataManager *coreDataManager;

@property (nonatomic, retain)NSMutableArray *collectionArray;

@property (nonatomic, retain)NSArray *tempArray;

@end

@implementation LuBinCollectionViewController

- (void)dealloc{
    [_tableView release];
    [_collectionArray release];
    [_tempArray release];
    [_coreDataManager release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArray = [NSMutableArray array];
        self.tempArray = [NSArray array];
        self.coreDataManager = [CoreDataManager shareCoreDataManager];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self downloadData];
    [self createCollectionViewController];
    // Do any additional setup after loading the view.
}

- (void)downloadData{
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"LuBinCollection"];
    self.tempArray = [_coreDataManager.managedObjectContext executeFetchRequest:request error:nil];
    self.collectionArray = [NSMutableArray arrayWithArray:self.tempArray];
    [self.tableView reloadData];
    [request release];
}

- (void)createCollectionViewController{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"相册收藏";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, CWIDTH, CHEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.collectionArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *collectionCell = @"collectionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:collectionCell];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:collectionCell];
        cell.textLabel.text = [[self.collectionArray objectAtIndex:indexPath.row] photo_des];
    }
    
    cell.selectionStyle = 0;
    return [cell autorelease];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [self.tableView setEditing:YES animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_coreDataManager.managedObjectContext deleteObject:self.collectionArray[indexPath.row]];
        [_coreDataManager saveContext];
        [self.collectionArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationBottom];
        [self.tableView reloadData];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumViewController *albumViewController = [[AlbumViewController alloc]init];
    [self.collectionDelegate  selectedAction:self.collectionArray[indexPath.row]];
    [self.navigationController pushViewController:albumViewController animated:YES];
    [albumViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
